package hehe.router.easy.rule;

import hehe.router.Controller;
import hehe.router.RequestUrl;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * url 规则类
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
abstract public class Rule
{
    /**
     * 类属性对象列表
     *<B>说明：</B>
     *<pre>
     * 缓存类属性对象
     *</pre>
     */
    protected static HashMap<String,Field> fields = new HashMap<>();

    public Rule(){};

    public Rule(Map<String,Object> attrs)
    {
        Rule.setAttribute(this,attrs);
    }

    /**
     * 设置对象属性
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static void setAttribute(Object obj,Map<String,Object> attrs)
    {
        Class clazz = obj.getClass();

        for (Map.Entry<String, Object> attr : attrs.entrySet()) {
            try {
                Field field = getField(clazz,attr.getKey());
                if (field != null) {
                    field.setAccessible(true);
                    field.set(obj,attr.getValue());
                }
            } catch (Exception e) {
                // 不做任何事情
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取对象属性
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param className class 对象
     * @param fieldName 属性名
     * @return 属性对象
     */
    private static Field getField(Class className, String fieldName)
    {
        String fieldKey = className.getName() + "." + fieldName;

        if (fields.containsKey(fieldKey)) {
            return fields.get(fieldKey);
        }

        // 读取所有父类及本来
        List<Class> superClazzs = findSuperclass(className);
        Field field = null ;
        for (int i = 0;i < superClazzs.size();i++)  {
            try {
                field = superClazzs.get(i).getDeclaredField(fieldName) ;
                if (field != null) {
                    // 找到属性,保持至至缓存
                    break;
                }
            } catch (NoSuchFieldException ex) {
                continue;
            }
        }

        if (field != null) {
            fields.put(fieldKey,field);
        }

        return field;
    }

    /**
     * 查找对应父类
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    private static List<Class> findSuperclass(Class<?> clazz)
    {
        Class<?> targetClazz = clazz;
        List<Class> superClazzs = new ArrayList<>();
        superClazzs.add(clazz);

        while (true) {//当父类为null的时候说明到达了最上层的父类(Object类).
            targetClazz = targetClazz.getSuperclass(); //得到父类,然后赋给自己
            if (targetClazz != null && targetClazz != Object.class) {
                superClazzs.add(targetClazz);
            } else {
                break;
            }
        }

        return superClazzs;
    }

    abstract public Controller parseRequest(String pathinfo);

    abstract public RequestUrl parseUrL(String url, Map<String,String> params);



}
